package com.kuaimacode.kframework.agent.controller.weather;

import com.github.pagehelper.PageInfo;
import com.kuaimacode.kframework.agent.services.weather.WeatherCfgService;
import com.kuaimacode.kframework.agent.services.weather.WeatherService;
import com.kuaimacode.kframework.agent.util.SessionUtil;
import com.kuaimacode.kframework.common.JsonResult;
import com.kuaimacode.kframework.constants.PageConstants;
import com.kuaimacode.kframework.mybatis.models.agent.SysAgentWeather;
import com.kuaimacode.kframework.mybatis.models.weather.WeatherCfg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.security.auth.Subject;
import java.util.Date;
import java.util.List;

/**
 * @author anzy
 * @date 2019/04/08
 */
/**
 * Controller
 */
@Controller
@RequestMapping({"/weather"})
public class WeatherController {
	private static String PAGE_FILE_NAME = "weather/";

	@Autowired
	private WeatherService weatherServices;


	@Autowired
	private WeatherCfgService weatherCfgService;


	@RequestMapping(value = {"/index"})
	public String index(@RequestParam(value = "pageNum", required = true, defaultValue = PageConstants.PAGE_NUM+"") int pageNum,
										@RequestParam(value = "pageSize", required = true, defaultValue = PageConstants.PAGE_SIZE+"") int pageSize,
						ModelMap model) {
		PageInfo<SysAgentWeather> pageInfo = weatherServices.findAll(pageNum, pageSize);
		// pageInfo.list weather
		List<SysAgentWeather> weathers = pageInfo.getList();
		//判断当前特殊天气是否正在进行
		for(SysAgentWeather weather :weathers){
			Date endTime = weather.getEndTime();
			Date startTime = weather.getStartTime();
			Date date = new Date();
			if(endTime.before(date)){//已过期
				weather.setStatus("0");
			} else if(startTime.after(date)){//未生效
				weather.setStatus("2");
			}else if(startTime.before(date)&&endTime.after(date)){//进行中
				weather.setStatus("1");
			}
		}
		pageInfo.setList(weathers);

		model.put("pageInfo", pageInfo);
		return PAGE_FILE_NAME + "index";
	}


	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String add(ModelMap map) {
		SysAgentWeather weather = new SysAgentWeather();
		//查询分站特殊天气配置
		List<WeatherCfg> weatherCfgs = weatherCfgService.queryAgentWeatherCfg(SessionUtil.getAgentId());

		map.put("weather", weather);
		map.put("isAdd", true);
		map.put("weatherList",weatherCfgs);
		return PAGE_FILE_NAME+"form";
	}

	@RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
	public String edit(@PathVariable String id, ModelMap map) {
		SysAgentWeather weather = weatherServices.getById(id);
		map.put("weather", weather);
		map.put("isAdd", false);
		return PAGE_FILE_NAME+"form";
	}

	@RequestMapping(value = {"/edit"}, method = RequestMethod.POST)
	@ResponseBody
	public JsonResult edit(SysAgentWeather weather, Boolean isAdd,ModelMap map) {
		try {
			if(isAdd){
				// 新建保存
				weather.setAgentId(SessionUtil.getAgentId());
				weather.setCreateAt(new Date());
				weather.setCreateBy(SessionUtil.getUserId());
				weatherServices.save(weather);
			}else{
				// 编辑保存
				weatherServices.updateById(weather);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return JsonResult.failure(e.getMessage());
		}
		return JsonResult.success();
	}


	@RequestMapping(value = "/detail/{id}", method = RequestMethod.GET)
	public String detail(@PathVariable String id, ModelMap map) {
		SysAgentWeather weather = weatherServices.getById(id);
		map.put("weather", weather);
		return PAGE_FILE_NAME+"detail";
	}

	@RequestMapping("/delete/{id}")
	@ResponseBody
	public JsonResult delete(@PathVariable String id, ModelMap map) {
		try {
			weatherServices.removeById(id);
		} catch (Exception e) {
			return JsonResult.failure(e.getMessage());
		}
		return JsonResult.success();
	}

	/**
	 * 立即停止当前特殊天气
	 * @param id
	 * @param map
	 * @return
	 */
	@RequestMapping("/stop/{id}")
	@ResponseBody
	public JsonResult stop(@PathVariable String id, ModelMap map) {
		try {
			SysAgentWeather weather = weatherServices.getById(id);
			weather.setEndTime(new Date());
			weather.setUpdateAt(new Date());
			weather.setUpdateBy(SessionUtil.getUserId());
			weatherServices.updateById(weather);
		} catch (Exception e) {
			return JsonResult.failure(e.getMessage());
		}
		return JsonResult.success();
	}

}
